/**
 * COPYRIGHT. Harry Wu 2011. ALL RIGHTS RESERVED.
 * Project: just-core
 * Author: Harry Wu <harrywu304@gmail.com>
 * Created On: Jun 19, 2011 3:25:29 PM
 *
*/ 
package org.justframework.core.helper;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * config helper
 *
 */
public class ConfigHelper {
	
	/**
	 * logger
	 */
	private static Logger logger = LoggerFactory.getLogger(ConfigHelper.class);
	
	/**
	 * xml root path
	 */
	private static String ROOT_PATH = "/just/";
	
	/**
	 * xml config document
	 */
	private static Document doc;
	
	/**
	 * xml configuration mapping
	 */
	private static Map<String,Object> cacheConfigs = new HashMap<String,Object>();
	
	static{
		//load justframework config
		InputStream is = null;
		try{
			SAXReader reader = new SAXReader();
			is = ConfigHelper.class.getResourceAsStream("/just.xml");
			doc = reader.read(is);
			logger.info("load just config success");	
		} catch(Exception e){
			logger.error("load just config fail",e);		
		} finally {
			if(is != null){
				try {is.close();} catch (IOException e) {}
			}
		}
	}
	
	/**
	 * return config item in string
	 * @param key xpath expression config key
	 * @return
	 */
	public static String getString(String key){
		key = ROOT_PATH + key;
		String result = null;
		if(!cacheConfigs.containsKey(key)){
			Node node = doc.selectSingleNode(key);
			if(node != null){
				result = node.getText();
			}
			cacheConfigs.put(key, result);
		} else {
			result = (String)cacheConfigs.get(key);
		}
		return result;
	}
	
	/**
	 * return config item in string array
	 * @param key xpath expression config key
	 * @return
	 */
	public static String[] getStringArray(String key){
		key = ROOT_PATH + key;
		String[] result = null;
		if(!cacheConfigs.containsKey(key)){
			List<Node> nodes = doc.selectNodes(key);
			if(nodes != null){
				int len = nodes.size();
				result = new String[len];
				int i = 0;
				for(Node node:nodes){
					result[i] = node.getText();
					i++;
				}
			}
			cacheConfigs.put(key, result);
		} else {
			result = (String[])cacheConfigs.get(key);
		}
		return result;
	}
	
	/**
	 * return config item in map
	 * @param key xpath expression config key
	 * @return
	 */
	public static Map<String,String> getMap(String key){
		key = ROOT_PATH + key + "/*";
		Map<String,String> result = null;
		if(!cacheConfigs.containsKey(key)){
			List<Node> nodes = doc.selectNodes(key);
			if(nodes != null){
				result = new HashMap<String,String>();
				for(Node node:nodes){
					result.put(node.getName(), node.getText());
				}
			}
			cacheConfigs.put(key, result);
		} else {
			result = (Map<String,String>)cacheConfigs.get(key);
		}
		return result;
	}

}
